#include "project.h"




void *pthread_A9_ctrl()
{
	struct msg msg_buf = {0, {0}};
	key_t key; 
	int msgid; 
	char device[6] = {0};

	memset(&msg_buf, 0, sizeof(msg_buf));
	int pwm_fd;
	int led_fd;
	if((pwm_fd = open("/dev/pwm", O_RDWR)) < 0)
		{
			perror("open PWM error");
			exit(1);
		}
	printf("open pwm success!\n");
	if((led_fd = open("/dev/led2",O_RDWR))< 0)
	{
			perror("open led error");
			exit(1);
	
	}
	printf("open led success!\n");
	serial_init(pwm_fd);
	serial_init(led_fd);

	if((key = ftok("/", 'b')) < 0)
	{
		perror("ftok");
		exit(1);
	}

	msgid = msgget(key,IPC_CREAT|IPC_EXCL|0666);
	if(msgid == -1)
	{
		if(errno == EEXIST)
		{
			msgid = msgget(key, 0666);
		}
		else{
			perror("msgget");
			exit(1);
		}
	}
	while(1)
	{
		msgrcv(msgid, &msg_buf, sizeof(msg_buf) - sizeof(long), 2L, 0);

		printf("%x\n",msg_buf.text[0]);


		if(msg_buf.text[0] == 1)   //蜂鸣器开

		{

			printf("%-----------\n");

			ioctl(pwm_fd, PWM_ON);
		}
		if(msg_buf.text[0] == 0) //蜂鸣器关
		{
			ioctl(pwm_fd, PWM_OFF);
		}


		if(msg_buf.text[0] == 0x11)
		{
			ioctl(led_fd, FS4412_LED2_ON);
		}

		if(msg_buf.text[0] == 0x10 )
		{          
			ioctl(led_fd, FS4412_LED2_OFF);
		}			
		if(msg_buf.text[0] == 0x22)
		{
			ioctl(led_fd, FS4412_LED3_ON);
		}

		if(msg_buf.text[0] == 0x20 )

		{
			ioctl(led_fd, FS4412_LED3_OFF);
		}




	}



}
